package com.example.administrator.fastdream.view.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.administrator.fastdream.R;
import com.example.administrator.fastdream.date.bean.DreamDetailBean;
import com.example.administrator.fastdream.date.presenter.DreamDetailPresenter;
import com.example.administrator.fastdream.view.base.BaseActivity;
import com.example.administrator.fastdream.view.view.CircleImageView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class DreamDetailActivity extends BaseActivity implements DreamDetailPresenter.DreamDetailCallBack {

    @BindView(R.id.iv_mine)
    ImageView ivMine;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.iv_head)
    CircleImageView ivHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_city)
    TextView tvCity;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_dream)
    TextView tvDream;
    @BindView(R.id.tv_money)
    TextView tvMoney;
    @BindView(R.id.tv_xuanyan)
    TextView tvXuanyan;
    @BindView(R.id.view_line)
    View viewLine;
    @BindView(R.id.tv_photo_count)
    TextView tvPhotoCount;
    @BindView(R.id.rl_photo)
    RelativeLayout rlPhoto;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerPhoto;
    private DreamDetailPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dream_detail);
        ButterKnife.bind(this);
        ivBack.setVisibility(View.VISIBLE);
        tvTitle.setText("圆梦详情");
        recyclerPhoto.setNestedScrollingEnabled(false);
        recyclerPhoto.setLayoutManager(new GridLayoutManager(this, 3));
        presenter = new DreamDetailPresenter(this, this);
        presenter.getDreamDetail(getIntent().getStringExtra("uid"));
    }

    @Override
    public void getDreamDetailInfo(DreamDetailBean dreamDetailBean) {
        DreamDetailBean.DataEntity.DataEntity2 dataEntity2 = dreamDetailBean.getData().getData().get(0);
        Glide.with(mContext).load(dataEntity2.getAvatar()).into(ivHead);
        tvName.setText(dataEntity2.getUsername());
        tvCity.setText(dataEntity2.getCity_name());
        tvAddress.setText(dataEntity2.getAddress());
        tvDream.setText(dataEntity2.getDream_name());
        tvMoney.setText(dataEntity2.getDream_money());
        tvXuanyan.setText(dataEntity2.getDream_declaration());
        List<String> dream_images = dataEntity2.getDream_images();
        if (dream_images == null || dream_images.size() == 0) {
            rlPhoto.setVisibility(View.GONE);
            recyclerPhoto.setVisibility(View.GONE);
        } else {
            tvPhotoCount.setText(dream_images.size() + "张");
            PhotoAdapter photoAdapter = new PhotoAdapter(R.layout.item_photo, dream_images);
            recyclerPhoto.setAdapter(photoAdapter);
        }
    }

    private class PhotoAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

        public PhotoAdapter(int layoutResId, @Nullable List<String> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, String item) {
            ImageView iv_photo = helper.getView(R.id.iv_photo);
            Glide.with(mContext).load(item).into(iv_photo);
        }
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }
}
